package com.workstudy.wstudy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.workstudy.common.core.domain.entity.SysUser;
import com.workstudy.wstudy.domain.Constants;
import com.workstudy.wstudy.domain.WstPost;
import com.workstudy.wstudy.service.IWstPostService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.workstudy.common.annotation.Log;
import com.workstudy.common.enums.BusinessType;
import com.workstudy.wstudy.domain.WstApply;
import com.workstudy.wstudy.service.IWstApplyService;
import com.workstudy.common.core.controller.BaseController;
import com.workstudy.common.core.domain.AjaxResult;
import com.workstudy.common.utils.poi.ExcelUtil;
import com.workstudy.common.core.page.TableDataInfo;

/**
 * 申请Controller
 * 
 * @author sun
 * @date 2021-04-05
 */
@Controller
@RequestMapping("/wstudy/apply")
public class WstApplyController extends BaseController
{
    private String prefix = "wstudy/apply";
    private String prefixPost = "wstudy/post";

    @Autowired
    private IWstApplyService wstApplyService;

    @Autowired
    private IWstPostService postService;

    @RequiresPermissions("wstudy:apply:view")
    @GetMapping()
    public String apply()
    {
        return prefix + "/apply";
    }

    @GetMapping("/examine")
    public String examine()
    {
        return prefix + "/apply_examine";
    }

    /**
     * 查询申请列表
     */
    @PostMapping("/list/student")
    @ResponseBody
    public TableDataInfo student(WstApply wstApply)
    {
        SysUser user = (SysUser)SecurityUtils.getSubject().getPrincipal();
        wstApply.setStudentId(user.getUserId());
        wstApply.setApplyStatus(Constants.ApplyStatus.IN_HANDING.toString());
        startPage();
        List<WstApply> list = wstApplyService.selectWstApplyList(wstApply);
        return getDataTable(list);
    }

    /**
     * 查询申请列表
     */
    @PostMapping("/list/teacher")
    @ResponseBody
    public TableDataInfo teacher(WstApply wstApply)
    {
        SysUser user = (SysUser)SecurityUtils.getSubject().getPrincipal();
        Map<String, Object> map = new HashMap<>();
        map.put("teacherId",user.getUserId());
        wstApply.setParams(map);
        wstApply.setApplyStatus(Constants.ApplyStatus.IN_HANDING.toString());
        wstApply.setStatus(Constants.ApprovalStatus.WAITING_FOR_APPROVAL.toString());
        startPage();
        List<WstApply> list = wstApplyService.selectWstApplyList2(wstApply);
        return getDataTable(list);
    }

    /**
     * 导出申请列表
     */
    @RequiresPermissions("wstudy:apply:export")
    @Log(title = "申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WstApply wstApply)
    {
        List<WstApply> list = wstApplyService.selectWstApplyList(wstApply);
        ExcelUtil<WstApply> util = new ExcelUtil<WstApply>(WstApply.class);
        return util.exportExcel(list, "apply");
    }

    /**
     * 新增申请
     */
    @GetMapping("/add/{postId}")
    public String add(@PathVariable("postId")Long postId,ModelMap map)
    {
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        WstPost post = postService.selectWstPostById(postId);
        map.put("postId",postId);
        map.put("user",user);
        map.put("post",post);
        return prefix + "/add";
    }

    /**
     * 新增保存申请
     */
    @Log(title = "申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WstApply wstApply)
    {
        return toAjax(wstApplyService.insertWstApply(wstApply));
    }

    /**
     * 修改申请
     */
    @GetMapping("/edit/{applyId}")
    public String edit(@PathVariable("applyId") Long applyId, ModelMap mmap)
    {
        WstApply wstApply = wstApplyService.selectWstApplyById(applyId);
        mmap.put("wstApply", wstApply);
        return prefix + "/edit";
    }

    /**
     * 详情
     */
    @GetMapping("/details/{applyId}")
    public String details(@PathVariable("applyId") Long applyId, ModelMap mmap)
    {
        WstApply wstApply = wstApplyService.selectWstApplyById(applyId);
        mmap.put("wstApply", wstApply);
        return prefix + "/details";
    }

    /**
     * 修改保存申请
     */
    @RequiresPermissions("wstudy:apply:edit")
    @Log(title = "申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WstApply wstApply)
    {
        return toAjax(wstApplyService.updateWstApply(wstApply));
    }

    /**
     * 删除申请
     */
    @RequiresPermissions("wstudy:apply:remove")
    @Log(title = "申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wstApplyService.deleteWstApplyByIds(ids));
    }

    /**
     * 学生资质校验
     * @return
     */
    @PostMapping("/studentPost")
    @ResponseBody
    public Boolean studentPost(WstApply apply)
    {
        return wstApplyService.studentPost(apply);
    }

    /**
     * 岗位审核页面
     * @return
     */
    @GetMapping("/examinePage/{applyId}")
    public String examinePage(@PathVariable("applyId")Long applyId,ModelMap map)
    {
        map.put("wstApply",wstApplyService.selectWstApplyById(applyId));
        return prefix + "/examine_page";
    }
}
